/*
 * @Description:
 * @Version: 1.0
 * @Autor: chen_yt
 * @Date: 2022-07-25 16:20:24
 * @LastEditors: chen_yt
 * @LastEditTime: 2022-08-04 14:19:44
 */
import store from '@/store'
import { CLIENT_CONFIG } from '@/store/State/stateTypes.js'
import { IS_SAAS_MODE } from '@/store/Getter/getterTypes.js'
import deepmerge from 'deepmerge'

/**
 * @description: 修改自己的应用配置，普通租户和管理平台限制的内容不同
 *               管理平台/非saas不做限制
 *               普通租户使用clientConfigLimit最大限制
 *               
 */
export function getOwnLimitClientConfig(originConfig) {
    if (store.state[CLIENT_CONFIG].tenantMode != 'tenant') {
        return originConfig
    }

    return getForceLimitClientConfig(originConfig, clientConfigLimit)
}


/**
 * @description: 通过管理平台修改普通租户的应用配置
 *               除普通用户自己的可编辑的内容外，还可以额外修改一些内容
 */
export function getSaasLimitClientConfigInMain(originConfig) {

    let configLimit = deepmerge(clientConfigLimit, extraClientConfigLimit)

    return getForceLimitClientConfig(originConfig, configLimit)
}

/**
 * @description: 强制限制应用配置内容
 *               
 */
export function getForceLimitClientConfig(originConfig, limit = clientConfigLimit) {
    if (!store.getters[IS_SAAS_MODE]) {
        return originConfig
    }
    if (!store.state[CLIENT_CONFIG].tenantMode) {
        return originConfig
    }

    let attr = {}

    Object.entries(originConfig).filter(([key, val]) => {
        if (!limit.hasOwnProperty(key)) {
            return false
        }
        //同时为object
        if (Object.prototype.toString.call(val) == "[object Object]" && Object.prototype.toString.call(limit[key]) == "[object Object]") {
            attr[key] = getForceLimitClientConfig(val, limit[key])
            return true
        }

        attr[key] = val

    })

    return attr
}

export const extraClientConfigLimit = {
    adminUserId: null
    , adminUserName: null
    , adminUserPassword: null
    , initMenuIdList: null
}

export const clientConfigLimit = {
    clientId: null
    , clientName: null
    , clientSecret: null
    , deleteFlag: 0
    , remark: '这里是备注'
    , ext1: null
    , ext2: null
    , ext3: null
    , ext4: null
    , ext5: null
    , id: null
    , isDefault: true
    , parantId: null
    , pathId: null
    , tenantId: 100000
    , webServerRedirectUri: null

    , additionalInformation: {
        "interfaceConfig": {
            "topMenu": 0,
            "tagsView": 0,
            "theme": "#2F54EB",
            "stripe": true,
            "websiteText": "fawkes",
            "logo": "./static/img/logo.png",
            "logoIconOffset": 0,
            "websiteLogo": "./favicon.ico",
            "copyright": "copyright@fawkes 2021",
            "footerView": true,
            "dateFormat": "",
            "timezone": "(UTC+08:00) 北京，重庆，香港特别行政区，乌鲁木齐",
            "language": "zh-CN"
        },
        "watermarkConfig": {
            "pageWatermark": false,
            "imgWatermark": true,
            "watermarkPosi": 8,
            "watermarkPrefix": "fawkes",
            "watermarkSuffix": 1
        },
        "advancedConfig": {
            "realTimePush": true,
            "socketType": "smqtt",
            "maxReconnect": 2,
            "reconnectInterval": 1000,
            "hierarchicalAuth": false,
            "multiPortal": true
        },
        "dataMode": {
            "user": "MASTER",
            "org": "MASTER",
            "dictionary": "MASTER"
        }
    }
}


